package com.kmxd.ams;

import cn.hutool.core.util.StrUtil;
import com.kmxd.ams.core.config.AmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetAddress;
import java.util.Set;

/**
 * App启动类
 *
 * @author admin
 */
@Slf4j
@EnableRetry
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
@MapperScan({"com.kmxd.ams.infra.**..mapper*"})
@ComponentScan("com.kmxd.ams.*")
@EnableConfigurationProperties({AmsConfig.class})
@SpringBootApplication(scanBasePackages = {"com.kmxd.ams","cn.hutool.extra.spring"})
public class RunAmsApplication extends SpringBootServletInitializer implements CommandLineRunner {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(RunAmsApplication.class);
  }

  public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext application =
            SpringApplication.run(RunAmsApplication.class, args);
    Environment env = application.getEnvironment();
    String ip = InetAddress.getLocalHost().getHostAddress();
    String port = env.getProperty("server.port");
    String path = env.getProperty("server.servlet.context-path");
    path = StrUtil.equals("/", path) ? "" : path;

    System.out.println("----------------------------------------------------------");
    System.out.println("Application upgrade-system is running! Access URLs:");
    System.out.println("Local:    http://localhost:" + port + path + "/");
    System.out.println("External: http://" + ip + ":" + port + path + "/");
    System.out.println("文档地址:  http://" + ip + ":" + port + path + "/doc.html");
    System.out.println("----------------------------------------------------------");
  }

  @Autowired private RedisTemplate redisTemplate;

  @Override
  public void run(String... args) throws Exception {
    // debug环境跳过清空缓存
    if (!log.isDebugEnabled()) {
      Set<String> keys = redisTemplate.keys("*");
      for (String key : keys) {
        redisTemplate.delete(key);
      }
      System.out.println("清除缓存成功");
    } else {
      log.debug("当前环境为测试环境，跳过清空缓存,线上请切换环境:{}", log.isDebugEnabled());
    }
  }
}
